package com.zzx.project.controller;

import com.zzx.project.entity.Comment;
import com.zzx.project.entity.User;
import com.zzx.project.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

//评论Controller
@Controller
@RequestMapping("comment")
public class CommentController {

    @Autowired
    CommentService commentService;

    //添加评论
    @RequestMapping("add")
    public String addComment(@RequestParam("aid") int aid, Comment comment, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        comment.setCommentuser(user.getUserid());
        comment.setCommentuserimg(user.getUserimg());
        comment.setCommentusername(user.getUsername());
        comment.setCommentarticleid(aid);
        int flag = commentService.addComment(comment);
        if (flag > 0) {
            return "redirect:/article/show/" + aid;
        } else {
            System.err.println("评论失败，请检查");
            return "error/5xx";
        }
    }
}
